<template>
  <el-dialog
    title="选择优惠券"
    :visible.sync="dialogVisible"
    width="300px">
    <div v-if="dialogVisible">
      优惠券列表:
      <el-select :value="couponId" style="width: 50%">
          <el-option :value="item.id" :label="'满'+item.fullMoney+',减'+item.reduceMoney" v-for="(item,index) in couponList" :key="index"></el-option>
      </el-select>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确认选择</el-button>
    </div>
  </el-dialog>

</template>

<script>
export default {
  name: 'ChooseCouponDialog',
  data: function () {
    return {
      callback:null,
      dialogVisible:false,
      productPrice:0,
      couponList:[],
      couponId:''
    }
  },
  mounted () {
    let $this = this
    $this.bus.$on('ChooseCouponDialogCallBus', function (productPrice,couponList,callback) {
      $this.dialogVisible = true
      $this.callback=callback
      $this.productPrice=productPrice
      $this.couponList=[]
      // console.log('couponList',couponList)
      // console.log('productPrice',productPrice)
      couponList.forEach(function (value, index) {
        if (value.fullMoney<=productPrice){
          $this.couponList.push(value)
        }
      })
      $this.couponId=$this.couponList[0].id
      $this.$forceUpdate()
    })

  },
  methods: {
    confirm:function () {
      if (typeof this.callback==='function'){
        var coupon={}
        let $this=this
        $this.couponList.forEach(function (value, index) {
          if (value.id===$this.couponId){
            coupon=value
          }
        })
        this.callback(coupon)
      }
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>

</style>
